import logger from '../core/logger';
import messageReply from '../model/comments/messageReply';
import returnModel from '../model/common/returnModel';
import apiInfo from './core/apiInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';

export class messageClient extends clientBase {
  constructor() {
    super();
  }

  getNotify(): Promise<returnModel<Array<messageReply>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://message.bilibili.com/api/notify/query.sysnotify.list.do", "&_ulv=10000&actionKey=appkey&data_type=1&page_size=40", null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            var data: Array<messageReply> = res.data;
            data.forEach((i) => {
              var item = i.content.match(/#{(.*?)}{""(.*?)""}/g);
              if (item[1].valueOf().length == 0) {
                i.notify = i.content;
              } else {
                i.link = item[2].valueOf();
                i.notify = `${item[1].valueOf()}${i.content.replace(item[0].valueOf(), '')}`;
              }
            });
            resolve(self.buildSuccessResult<Array<messageReply>>(data));
          } else
            resolve(self.buildFailedResult<Array<messageReply>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<Array<messageReply>>(e));
      }
    });
  }

  getPraise(): Promise<returnModel<Array<messageReply>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("http://message.bilibili.com/api/notify/query.praiseme.list.do", "&_ulv=10000&actionKey=appkey&data_type=1&page_size=40", null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            var data: Array<messageReply> = res.data;
            resolve(self.buildSuccessResult<Array<messageReply>>(data));
          } else
            resolve(self.buildFailedResult<Array<messageReply>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<Array<messageReply>>(e));
      }
    });
  }

  getAtMe(): Promise<returnModel<Array<messageReply>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("http://message.bilibili.com/api/notify/query.atme.list.do", "&_ulv=10000&actionKey=appkey&data_type=1&page_size=40", null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            var data: Array<messageReply> = res.data;
            resolve(self.buildSuccessResult<Array<messageReply>>(data));
          } else
            resolve(self.buildFailedResult<Array<messageReply>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<Array<messageReply>>(e));
      }
    });
  }

  getReply(): Promise<returnModel<Array<messageReply>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("http://message.bilibili.com/api/notify/query.replyme.list.do", "&_ulv=10000&data_type=1&page_size=40", null, true);
        info.buildVer = apiProvider.Build4;
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            var data: Array<messageReply> = res.data;
            resolve(self.buildSuccessResult<Array<messageReply>>(data));
          } else
            resolve(self.buildFailedResult<Array<messageReply>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<Array<messageReply>>(e));
      }
    });
  }
}
export default new messageClient();